//
//  FbeeImageButton.m
//  FbeeAPP
//
//  Created by dev-m on 2018/3/15.
//  Copyright © 2018年 mohao. All rights reserved.
//

#import "FbeeImageButton.h"

@interface FbeeImageButton()
{
    UIButton *btn;
    UILabel *info;
}

@end

@implementation FbeeImageButton


-(instancetype)initWithFrame:(CGRect)frame imageName:(NSString *)imageName
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        //image size 15*15
        
        btn = [UIButton makeButton:^(TTButtonExtend *make) {
            make.setFrame(CGRectMake(0, 0, self.width, self.height));
            make.addImage([UIImage imageNamed:imageName],UIControlStateNormal);
            make.addTarget(self,@selector(btnAction));
            make.imageEdgeInsets = UIEdgeInsetsMake((self.height - 15)/2, 0, (self.height - 15)/2, self.width - 15);
            [self addSubview:make];
        }];
        
        info = [UILabel makeLabel:^(TTLabelExtend *make) {
            make.addTextColor(FBEE_COLOR_INFO).addFont([UIFont systemFontOfSize:12]);
            make.height = self.height;
            make.width = self.width - 15 - 6;
            make.top = 0;
            make.left = 15 + 6;
            [self addSubview:make];
        }];
        
        
    }
    
    return self;
}

-(void)btnAction
{
    if (self.onClick) {
        self.onClick();
    }
}

-(void)setTtile:(NSString *)title
{
    info.text = title;
}

-(void)btnSizeToFit
{
    [info sizeToFit];
    info.height = self.height;
    if (info.width > 100) {
        info.width = 100;
    }
    self.width = info.right;
    btn.width = self.width;
    btn.imageEdgeInsets = UIEdgeInsetsMake((self.height - 15)/2, 0, (self.height - 15)/2, self.width - 15);
}

@end
